#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>

class TinyWebDB {
private:
    String serverUrl;
    String user;
    String secret;

public:
    TinyWebDB() {}

    void setServerParameter(const String& url, const String& u, const String& s) {
        serverUrl = url;
        user = u;
        secret = s;
    }

    // 更新标签值
    void updateTagValue(const String& tag, const String& tagValue) {
        if (WiFi.status() == WL_CONNECTED) {
            HTTPClient http;
            String fullUrl = serverUrl + "?action=update&tag=" + tag + "&value=" + tagValue;
            http.begin(fullUrl);
            int httpResponseCode = http.GET();

            if (httpResponseCode > 0) {
                Serial.print("HTTP Response code for update: ");
                Serial.println(httpResponseCode);
            } else {
                Serial.print("Error on update. Code: ");
                Serial.println(httpResponseCode);
            }

            http.end();
        } else {
            Serial.println("WiFi disconnected");
        }
    }

    // 获取标签值
    String getTag(const String& tag) {
        String result = "";
        if (WiFi.status() == WL_CONNECTED) {
            HTTPClient http;
            String fullUrl = serverUrl + "?action=get&tag=" + tag;
            http.begin(fullUrl);
            int httpResponseCode = http.GET();

            if (httpResponseCode > 0) {
                Serial.print("HTTP Response code for get: ");
                Serial.println(httpResponseCode);
                result = http.getString();
            } else {
                Serial.print("Error on get. Code: ");
                Serial.println(httpResponseCode);
            }

            http.end();
        } else {
            Serial.println("WiFi disconnected");
        }
        return result;
    }

    // 删除标签
    void deleteTag(const String& tag) {
        if (WiFi.status() == WL_CONNECTED) {
            HTTPClient http;
            String fullUrl = serverUrl + "?action=delete&tag=" + tag;
            http.begin(fullUrl);
            int httpResponseCode = http.GET();

            if (httpResponseCode > 0) {
                Serial.print("HTTP Response code for delete: ");
                Serial.println(httpResponseCode);
            } else {
                Serial.print("Error on delete. Code: ");
                Serial.println(httpResponseCode);
            }

            http.end();
        } else {
            Serial.println("WiFi disconnected");
        }
    }

    // 计数库总数
    int count() {
        int result = 0;
        if (WiFi.status() == WL_CONNECTED) {
            HTTPClient http;
            String fullUrl = serverUrl + "?action=count";
            http.begin(fullUrl);
            int httpResponseCode = http.GET();

            if (httpResponseCode > 0) {
                Serial.print("HTTP Response code for count: ");
                Serial.println(httpResponseCode);
                result = http.getString().toInt();
            } else {
                Serial.print("Error on count. Code: ");
                Serial.println(httpResponseCode);
            }

            http.end();
        } else {
            Serial.println("WiFi disconnected");
        }
        return result;
    }

    // 搜索标签
    String searchTags(int startNum, int num, const String& tag, const String& showtype) {
        String result = "";
        if (WiFi.status() == WL_CONNECTED) {
            HTTPClient http;
            String fullUrl = serverUrl + "?action=search&no=" + String(startNum) + "&count=" + String(num) + "&tag=" + tag + "&type=" + showtype;
            http.begin(fullUrl);
            int httpResponseCode = http.GET();

            if (httpResponseCode > 0) {
                Serial.print("HTTP Response code for search: ");
                Serial.println(httpResponseCode);
                result = http.getString();
            } else {
                Serial.print("Error on search. Code: ");
                Serial.println(httpResponseCode);
            }

            http.end();
        } else {
            Serial.println("WiFi disconnected");
        }
        return result;
    }
};